/**
 * 商品数据种子脚本
 * 用于创建测试商品数据
 */

const mongoose = require('mongoose');
const Product = require('../src/models/product.model');
require('dotenv').config();

const MONGODB_URI = process.env.MONGODB_URI || 'mongodb+srv://2264521353:1234567890@six0.hic1spu.mongodb.net/goshop';

// 测试商品数据
const sampleProducts = [
  {
    name: 'iPhone 15 Pro Max',
    description: '全新iPhone 15 Pro Max，搭载A17 Pro芯片，支持5G网络，拍照更清晰，续航更持久。采用钛金属设计，更轻更坚固。',
    price: 9999,
    originalPrice: 10999,
    mainImage: 'https://via.placeholder.com/400x400/333/fff?text=iPhone+15+Pro',
    images: [
      'https://via.placeholder.com/400x400/333/fff?text=iPhone+15+Pro+1',
      'https://via.placeholder.com/400x400/333/fff?text=iPhone+15+Pro+2',
      'https://via.placeholder.com/400x400/333/fff?text=iPhone+15+Pro+3'
    ],
    brand: 'Apple',
    category: '手机数码',
    subcategory: '智能手机',
    tags: ['5G', '拍照', 'A17芯片', '钛金属'],
    countInStock: 50,
    rating: 4.8,
    numReviews: 128,
    isFeatured: true,
    isOnSale: true,
    salePrice: 9999,
    salesCount: 1200,
    specifications: {
      '屏幕尺寸': '6.7英寸',
      '处理器': 'A17 Pro',
      '存储容量': '256GB',
      '摄像头': '4800万像素',
      '电池容量': '4441mAh'
    },
    specs: {
      '颜色': ['深空黑色', '银色', '金色', '深紫色'],
      '容量': ['128GB', '256GB', '512GB', '1TB']
    }
  },
  {
    name: '小米14 Ultra',
    description: '小米14 Ultra，徕卡专业影像，骁龙8 Gen3处理器，120W快充，2K屏幕，专业摄影旗舰。',
    price: 5999,
    originalPrice: 6499,
    mainImage: 'https://via.placeholder.com/400x400/FF6900/fff?text=小米14+Ultra',
    images: [
      'https://via.placeholder.com/400x400/FF6900/fff?text=小米14+Ultra+1',
      'https://via.placeholder.com/400x400/FF6900/fff?text=小米14+Ultra+2'
    ],
    brand: '小米',
    category: '手机数码',
    subcategory: '智能手机',
    tags: ['徕卡', '骁龙8Gen3', '120W快充', '2K屏'],
    countInStock: 80,
    rating: 4.7,
    numReviews: 89,
    isFeatured: true,
    isOnSale: true,
    salePrice: 5999,
    salesCount: 856,
    specifications: {
      '屏幕尺寸': '6.73英寸',
      '处理器': '骁龙8 Gen3',
      '存储容量': '512GB',
      '摄像头': '5000万像素徕卡',
      '电池容量': '5300mAh'
    },
    specs: {
      '颜色': ['雅川青', '昆仑白', '岩石黑'],
      '容量': ['256GB', '512GB', '1TB']
    }
  },
  {
    name: 'MacBook Pro 14英寸',
    description: 'MacBook Pro 14英寸，M3 Pro芯片，18小时电池续航，Liquid Retina XDR显示屏，专业创作利器。',
    price: 15999,
    originalPrice: 16999,
    mainImage: 'https://via.placeholder.com/400x400/333/fff?text=MacBook+Pro+14',
    images: [
      'https://via.placeholder.com/400x400/333/fff?text=MacBook+Pro+14+1',
      'https://via.placeholder.com/400x400/333/fff?text=MacBook+Pro+14+2'
    ],
    brand: 'Apple',
    category: '电脑办公',
    subcategory: '笔记本电脑',
    tags: ['M3芯片', '专业创作', 'Retina显示屏', '长续航'],
    countInStock: 25,
    rating: 4.9,
    numReviews: 45,
    isFeatured: true,
    isOnSale: false,
    salesCount: 234,
    specifications: {
      '屏幕尺寸': '14.2英寸',
      '处理器': 'M3 Pro',
      '内存': '18GB',
      '存储': '512GB SSD',
      '续航': '18小时'
    }
  },
  {
    name: 'AirPods Pro 2',
    description: 'AirPods Pro 2代，主动降噪，空间音频，H2芯片，最长6小时聆听时间，无线充电盒。',
    price: 1899,
    originalPrice: 1999,
    mainImage: 'https://via.placeholder.com/400x400/fff/333?text=AirPods+Pro+2',
    images: [
      'https://via.placeholder.com/400x400/fff/333?text=AirPods+Pro+2+1'
    ],
    brand: 'Apple',
    category: '手机数码',
    subcategory: '耳机音响',
    tags: ['主动降噪', '空间音频', 'H2芯片', '无线充电'],
    countInStock: 100,
    rating: 4.6,
    numReviews: 267,
    isFeatured: false,
    isOnSale: true,
    salePrice: 1899,
    salesCount: 1567,
    specifications: {
      '芯片': 'H2',
      '降噪': '主动降噪',
      '续航': '6小时+24小时',
      '充电': '无线充电',
      '防水': 'IPX4'
    },
    specs: {
      '颜色': ['白色', '黑色']
    }
  },
  {
    name: '小米电视65英寸',
    description: '小米电视65英寸4K超高清，120Hz刷新率，杜比视界，MEMC运动补偿，智能语音控制。',
    price: 2999,
    originalPrice: 3499,
    mainImage: 'https://via.placeholder.com/400x400/000/fff?text=小米电视65寸',
    images: [
      'https://via.placeholder.com/400x400/000/fff?text=小米电视65寸+1'
    ],
    brand: '小米',
    category: '家用电器',
    subcategory: '电视',
    tags: ['4K', '120Hz', '杜比视界', '智能语音'],
    countInStock: 30,
    rating: 4.5,
    numReviews: 156,
    isFeatured: false,
    isOnSale: true,
    salePrice: 2999,
    salesCount: 445,
    specifications: {
      '屏幕尺寸': '65英寸',
      '分辨率': '4K超高清',
      '刷新率': '120Hz',
      '系统': 'MIUI TV',
      '接口': 'HDMI 2.1'
    }
  },
  {
    name: '华为MateBook X Pro',
    description: '华为MateBook X Pro，13.9英寸3K触控屏，Intel第12代酷睿处理器，轻薄便携，商务办公首选。',
    price: 8999,
    originalPrice: 9999,
    mainImage: 'https://via.placeholder.com/400x400/1976D2/fff?text=华为MateBook',
    images: [
      'https://via.placeholder.com/400x400/1976D2/fff?text=华为MateBook+1'
    ],
    brand: '华为',
    category: '电脑办公',
    subcategory: '笔记本电脑',
    tags: ['3K触控屏', '轻薄', '商务办公', '长续航'],
    countInStock: 40,
    rating: 4.4,
    numReviews: 78,
    isFeatured: false,
    isOnSale: true,
    salePrice: 8999,
    salesCount: 189,
    specifications: {
      '屏幕尺寸': '13.9英寸',
      '分辨率': '3K触控',
      '处理器': 'Intel i7-1260P',
      '内存': '16GB',
      '存储': '1TB SSD'
    }
  }
];

async function seedProducts() {
  try {
    console.log('🔗 连接到数据库...');
    await mongoose.connect(MONGODB_URI, {
      dbName: 'goshop',
      serverSelectionTimeoutMS: 5000,
      socketTimeoutMS: 45000,
      family: 4
    });

    console.log('✅ 数据库连接成功');

    // 清空现有商品数据
    console.log('🧹 清理现有商品数据...');
    await Product.deleteMany({});
    console.log('✅ 清理完成');

    // 插入新的商品数据
    console.log('📦 插入商品数据...');
    const products = await Product.insertMany(sampleProducts);
    console.log(`✅ 成功插入 ${products.length} 个商品`);

    // 显示插入的商品信息
    console.log('\n📋 插入的商品列表:');
    products.forEach((product, index) => {
      console.log(`${index + 1}. ${product.name} - ¥${product.price} (${product.brand})`);
    });

    console.log('\n✅ 商品数据种子完成');

  } catch (error) {
    console.error('❌ 商品数据种子失败:', error);
  } finally {
    await mongoose.disconnect();
    console.log('🔌 数据库连接已断开');
    process.exit(0);
  }
}

// 运行种子脚本
seedProducts();
